<?php
class Zendvn_Model_Table_ProductCategories extends Zendvn_Model_Nested{
	
	protected 	$_name 		= "product_categories";
	protected 	$_primary 	= "id"; 
	protected 	$_listSQL; 
	public 		$_alias 	= 'node';
	
	public $_searchFields 	= array(
									'node.name'=>'- Name',
									'node.id'=>'- ID',
								  	'node.content'=>'- Content',
								  	'node.created'=>'- Created',
								  	'u1.email'=>'- Created by',
								  	'node.modified'=>'- Modified',
								  	'u2.email'=>'- Modified by',
									);
	
	
	public function itemInSelect($arrParam = null,$options = null){
		//====================== Nested =======================		
		$db = $this->getAdapter();
		if($options == null){
			$select 			= $db->select()->from($this->_name)->order('lft ASC');
			$result 			= $db->fetchAll($select);
			//array_unshift($result, array('id'=>1,'name'=>'-- ROOT --'));
		}
		if($options['task'] == 'admin-add'){
			$select 			= $db->select()->from($this->_name)->order('lft ASC');
			$result 			= $db->fetchAll($select);
			//array_unshift($result, array('id'=>1,'name'=>'-- ROOT --'));
		}
		if($options['task'] == 'product'){
			$select 			= $db->select()->from($this->_name)->where('level > 0')->order('lft ASC');
			$result 			= $db->fetchAll($select);
			array_unshift($result, array('name'=>'-- Select category --'));
		}
		
		if($options['task'] == 'cat-index'){
			$select 			= $db->select()->from($this->_name)->where('level = 1')->where('status = "active"')->order('lft ASC');
			$result 			= $db->fetchAll($select);
		}
		return $result;
	}
	
	public function sortItem($arrParam = null,$options = null){
		if($options == null){
			if(!empty($arrParam['cid'])){
				$db = $this->getDefaultAdapter();
				$select = $db->select()
							->from($this->_name)
							->order('lft ASC');
				$result = $db->fetchAll($select);
				$data = array();
				foreach ($result as $v){
					$data[] = $v;
				}
				$orderArr = $arrParam['ordering'];
				$this->orderTree($data, $orderArr);
			}
		}
	}
	
	public function statusItem($arrParam = null,$options = null){
		if($options == null){
			$status = ($arrParam['s'] == 'active' )? 'inactive':'active';	
			
			$children = $this->getChildrenNode($arrParam['id'],array('node'=>true));
			if(!empty($children)){	
				foreach ($children as $val):
					$data = array('status'=>$status);
					$where = 'id = ' . (int)$val['id'];
					$this->update($data, $where);
				endforeach;
			}
		}
		
		if($options['task'] == 'multi'){
			
			$status = $arrParam['s'];				
			if(count($arrParam['cid'])>0){
				foreach ($arrParam['cid'] as $val){
					$fields = array('id');
					$children = $this->getChildrenNode($val,array('node'=>true,'fields'=>$fields));
					
					if(!empty($children)){	
						foreach ($children as $val):
							$data = array('status'=>$status);
							$where = 'id = ' . (int)$val['id'];
							$this->update($data, $where);
						endforeach;
					}
					
				}
			}
		}
	}
	public function deleteItem($arrParam = null,$options = null){
		if($options == null){
			if((int)$arrParam['id']>0){
				$children = $this->getChildrenNode($arrParam['id'],array('fields'=>array('image'),'node'=>true));
				if(!empty($children)){	
					$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
													 	  		'fileConfig'=>'product-category.ini'));
					
					$file = new Zendvn_File_Upload();
					$imageConfig = $config->getConfig('category');
					foreach ($children as $images){
						$file->remove($imageConfig['uploadDir'],$images['image']);
						$file->remove($imageConfig['thumbDir'],$images['image']);
						
					}
					$this->removeNode($arrParam['id'], 'branch');
				}
			}
		}
		if($options['task'] == 'multi'){		
			if(count($arrParam['cid'])>0){	
				foreach ($arrParam['cid'] as $val){
					$children = $this->getChildrenNode($val,array('fields'=>array('image'),'node'=>true));
					if(!empty($children)){	
						$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
														 	  		'fileConfig'=>'product-category.ini'));
						
						$file = new Zendvn_File_Upload();
						$imageConfig = $config->getConfig('category');
						foreach ($children as $images){
							$file->remove($imageConfig['uploadDir'],$images['image']);
							$file->remove($imageConfig['thumbDir'],$images['image']);
							
						}
						$this->removeNode($val, 'branch');
					}
				}
			}
		}
	}
	
	public function getItem($arrParam = null,$options = null){
		if($options == null){
			if(!empty($arrParam['id'])){
				$result = $this->getNodeInfo($arrParam['id']);
			}
		}
		
		if($options['task'] == 'info'){
			$db = $this->getAdapter();
			$select = $db->select()
						->from($this->_name . ' AS c')
						->joinLeft($this->_name . ' AS c2', 'c2.id = c.parents', array('c2.name AS parents'))
						->joinLeft('users AS u1', 'u1.id = c.created_by', array('u1.email AS createdBy'))
						->joinLeft('users AS u2', 'u2.id = c.modified_by', array('u2.email AS modifiedBy'))
						->where('c.id = ?',$arrParam['id'],1);
			
			$result = $db->fetchRow($select);
		}
		return $result;
	}
	
	public function listItemAll($arrParam = null,$options = null){
		if($options == NULL){
			$select = $this->_sqlItem;
			$select->joinLeft('users AS u1', 'u1.id = node.created_by', array('u1.email AS createdBy'))
					->joinLeft('users AS u2', 'u2.id = node.modified_by', array('u2.email AS modifiedBy'));
			$arrFilter = $arrParam['arrFilter'];
			
			if(!empty($arrFilter['col'])){
				$select->order($arrFilter['col'] . ' ' . $arrFilter['order']);			
			}
			if(!empty($arrFilter['status']) && $arrFilter['status'] != 'none'){
				$select->where($this->_alias . '.status = ?',$arrFilter['status']);
			}
			if($arrFilter['level']>0){
				$select->where($this->_alias . '.level <= ?',$arrFilter['level'],1);
			}
			if($arrFilter['parentNode']>1){
				$nodeInfo = $this->getNodeInfo($arrFilter['parentNode']);			
				$select->where('lft >= ?', $nodeInfo['lft'],1);
				$select->where('rgt <= ?', $nodeInfo['rgt'],1);
			}
			if(!empty($arrFilter['keyword']) && !empty($arrFilter['searchField'])){
				$keyword = '%' . $arrFilter['keyword'] . '%';
				$select->where($arrFilter['searchField'] . ' LIKE ?', $keyword);
			}
			$this->_sqlItem = $select;
			$result = $this->listItem(0, 'all');
		}
		
		if($options['task'] == 'main-category'){
			$select = $this->_sqlItem;
			$select->joinLeft('users AS u1', 'u1.id = node.created_by', array('u1.email AS createdBy'))
					->joinLeft('users AS u2', 'u2.id = node.modified_by', array('u2.email AS modifiedBy'))
					->where('node.status = "active"')
					->where('node.parents != 0');
			$result = $this->listItem(0, 'all');
		}
		if($options['task'] == 'filter-category'){
			$select = $this->_sqlItem;
			$select	->where('node.status = "active"')
					->where('node.parents = ?',$arrParam['id']);
			$result = $this->listItem(0, 'all');
		}
		
		return $result;
	}	
	
	public function totalItem($arrParam = null,$options = null){
		$result = $this->totalNode() - 1;
		return $result;
		
	}
	
	public function saveItem($arrParam = null, $options = null){			
		$info = new Zendvn_System_Info();
		$idUser = $info->getMemberInfo('id');
		
		if(!empty($arrParam['id'])){
			//=================== Edit Action ==================
			$data 			= array(
									'name' 			=> $arrParam['name'],
									'image' 		=> $arrParam['image'],
									'html_descript' => $arrParam['html_descript'],
									'html_descript' => $arrParam['html_descript'],
									'content' 		=> $arrParam['content'],
									'params' 		=> $arrParam['params'],
									'status' 		=> $arrParam['status'],
									'modified' 		=> date('Y-m-d H:i:s'),
									'modified_by' 	=> $idUser,
								    );
			$this->updateNode($data, $arrParam['id'], $arrParam['parents']);
		}else{
			//==================== Add Action ==================
			$data 	= array(
							'name' 			=> $arrParam['name'],
							'image' 		=> $arrParam['image'],
							'html_descript' => $arrParam['html_descript'],
							'html_descript' => $arrParam['html_descript'],
							'content' 		=> $arrParam['content'],
							'params' 		=> $arrParam['params'],
							'status' 		=> $arrParam['status'],
							'created' 		=> date('Y-m-d H:i:s'),
							'created_by' 	=> $idUser,
						 	);
			$this->insertNode($data, $arrParam['parents'], $options);
		}
	}
}